#include <cstdio>
#include <cstring>

using namespace std;

typedef long long ll;
const int maxk=300, maxn=301, mods=1000000007;

ll fact[maxk*maxn+1], invf[maxk*maxn+1];

ll qpower(ll a, int n) {
    ll s=1;
    for (; n; n/=2) {
        if (n&1) s=s*a%mods;
        a=a*a%mods;
    }
    return s;
}

void initFact(int n) {
    fact[0] = 1;
    for (int i=1; i<=n; i++) fact[i] = fact[i-1]*i%mods;
    invf[n] = qpower(fact[n], mods-2);
    for (int i=n; i>0; i--) invf[i-1] = invf[i]*i%mods;
}

int main() {
    freopen("nothard.in", "r", stdin);
    freopen("check.out", "w", stdout);

    static int a[maxk+1][maxn+1], p[maxn+1][maxk+1];
    int k, n;
    scanf("%d %d", &k, &n);
    initFact(n*k);
    for (int i=1; i<=k; i++) {
        for (int j=1; j<=n; j++) {
            scanf("%d", a[i]+j);
            p[i][a[i][j]] = j;
        }
        a[i][n+1] = p[i][n+1] = n+1;
    }
    n++;

    static int cor[maxn+1][maxn+1];
    static ll f[maxn+1];
    ll ans=0, now=0;
    for (int i=1; i<=k; i++) {
        for (int j=1; j<=n; j++) {
            cor[j][0] = 0;
            for (int t=1; t<=n; t++) {
                if (j!=t) {
                    cor[j][++cor[j][0]] = t;
                }
            }
        }

        for (int j=i; j<=k; j++) {
            static int temp[maxn+1];
            static bool book[maxn+1];
            memset(book, 0, sizeof book);
            for (int x=1; x<=n; x++) {
                ll u=0, v=fact[j-i+1];
                for (int y=i; y<=j; y++) {
                    u += p[y][a[j][x]]-1;
                    v = v*invf[p[y][a[j][x]]-1]%mods;
                }
                f[a[j][x]] = fact[u]*v%mods;

                memcpy(temp, cor[a[j][x]], sizeof temp);
                cor[a[j][x]][0] = 0;
                for (int y=1; y<=temp[0]; y++) {
                    if (book[temp[y]]) {
                        cor[a[j][x]][++cor[a[j][x]][0]] = temp[y];

                        u=0, v=fact[j-i+1];
                        for (int z=i; z<=j; z++) {
                            int t=p[z][a[j][x]]-p[z][temp[y]]-1;
                            u += t;
                            v = v*invf[t]%mods;
                        }
                        f[a[j][x]] = (f[a[j][x]]+mods-f[temp[y]]*fact[u]%mods*v%mods)%mods;
                    }
                }
                book[a[j][x]] = true;
            }

            ans = (ans+f[n]*invf[j-i+1]%mods)%mods;
        }
    }

    printf("%lld", ans);

    fclose(stdin);
    fclose(stdout);
    return 0;
}
